package cz.muni.fi.pa165.ddtroops.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

/**
 *  Squad class represents a squad of Heros in D&D like games. Squad groups heros as its members,
 *  one of the members is set as leader of squad. Squad has its own gold, task set, description and name.
 * @author Jan Šťastný
 */

@Entity
public class Squad implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String description;
    private String task;
    private Long gold;
    // member specification is in special SQUAD_HERO table
    @OneToMany
    private List<Hero> members;
    // leader specification is in squad's table
    @OneToOne
    private Hero leader;

    public Squad(){
        this.members = new ArrayList<Hero>();
    }
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public Long getGold() {
        return gold;
    }

    public void setGold(Long gold) {
        this.gold = gold;
    }

    public List<Hero> getMembers() {
        return members;
    }

    public void setMembers(List<Hero> members) {
        this.members = members;
    }

    public void addMember(Hero member){
        if(!members.contains(member)){
            members.add(member);
        }
    }
    public void removeMember(Hero member){
        if(getLeader().equals(member)){
            throw new IllegalArgumentException("Cannot remove member who is leader of the group.");
        }
        members.remove(member);
    }

    public Hero getLeader() {
        return leader;
    }
    /**
     * Method setLeader sets a hero as squad's leader
     * @param leader hero to be set as leader
     * @throws IllegalArgumentException when hero is not member of the squad.
     */
    public void setLeader(Hero leader) {
        if(!members.contains(leader)){
            throw new IllegalArgumentException("Given hero is not member of the group, he can't be its leader.");
        }
        this.leader = leader;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Squad)) {
            return false;
        }
        Squad other = (Squad) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "cz.muni.fi.pa165.ddtroops.Group[ id=" + id + " ]";
    }

}
